package com.haotunet.app.goldtreasure.views;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.model.Shop;
import com.haotunet.app.goldtreasure.other.Interface.OnShopClickListener;

import java.util.List;

public class ShopPopupView extends PopupWindow implements OnClickListener, OnTouchListener {
	private Context mContext;
	private Activity mActivity;
	private View rootView;
	private ListView mListView;
	private LayoutInflater mInflater;
	private List<Shop> mlist;
	private OnShopClickListener mListener;

	public ShopPopupView(Context context, Activity activity, List<Shop> list) {
		this.mContext = context;
		mActivity = activity;
		mlist = list;
		mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		initView();
	}

	private void initView() {
		rootView = mInflater.inflate(R.layout.resevertime_popup_view, null);
		setContentView(rootView);
		mListView = (ListView) rootView.findViewById(R.id.lv_resever_time);
		mListView.setAdapter(new MyAdapter());
		setFocusable(true);
		setAnimationStyle(R.style.AnimBottom);
		setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
		setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
		setBackgroundDrawable(new BitmapDrawable());
		rootView.setOnTouchListener(this);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		int height = rootView.findViewById(R.id.resever_time_pop_layout).getTop();
		int y = (int) event.getY();
		if (event.getAction() == MotionEvent.ACTION_UP) {
			if (y < height) {
				dismiss();
			}
		}
		return true;
	}

	@Override
	public void onClick(View v) {

	}

	class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return mlist.size();
		}

		@Override
		public Object getItem(int position) {
			return mlist.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = mInflater.inflate(R.layout.resever_time_item, null);
				holder.tvValue = (TextView) convertView.findViewById(R.id.tv_value);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			final Shop shop = mlist.get(position);
			holder.tvValue.setText(shop.getShopName());
			convertView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					mListener.onValueSet(shop);
					dismiss();
				}
			});
			return convertView;
		}

	}

	class ViewHolder {
		TextView tvValue;
	}

	public void setOnShopClickListener(OnShopClickListener listener) {
		mListener = listener;
	}
}
